// pages/addressform/addressform.js
import qqmap from "../../utils/qqmap"
import instance from "../../utils/util"
Page({

    /**
     * 页面的初始数据
     */
    data: {
        id:"",
        checked: true,
        area: "",

        name: "",
        phone: "",
        provinceName: "",
        provinceCode: "",
        cityName: '',
        cityCode: '',
        districtName: '',
        districtCode: '',
        area: "",
        fullAddress: "",
        isDefault: 0,

    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        // this.onLocation()
        console.log(options)
        this.getdata(options.id)
    },
    async getdata(e) {
        console.log(e)
        const res = await instance.get(`/userAddress/${e}`);
        console.log(res)
        this.setData({
            name: res.data.name,
            area: res.data.area,

id:res.data.id,
            phone: res.data.phone,
            provinceName: res.data.provinceName,
            provinceCode: res.data.provinceCode,
            cityName: res.data.cityName,
            cityCode: res.data.cityCode,
            districtName: res.data.districtName,
            districtCode: res.data.districtCode,
            area: res.data.code,
            fullAddress: res.data.fullAddress,
            isDefault: res.data.isDefault,
        })
        if(res.data.isDefault == 0){
            this.setData({
                checked:true
            })
        }else{
            this.setData({
                checked:false
            })
            
        }

    },
    async onLocation() {
        const {
            latitude,
            longitude
        } = await wx.chooseLocation();
        this.getPoint(latitude, longitude);

    },
    onChange(e) {
        console.log(e)
        if (e.detail == true) {
            this.setData({
                isDefault: 0
            })
        } else {
            this.setData({
                isDefault: 1
            })

        }
        this.setData({
            checked: e.detail
        })
    },
    async dqwz() {
        const {
            latitude,
            longitude
        } = await wx.getLocation();
        this.getPoint(latitude, longitude);

    },
    getPoint(a, b) {
        wx.showLoading({
            title: '正在加载',
            mask: true
        })
        qqmap.reverseGeocoder({
            location: [a, b].join(","),
            success: (res) => {
                console.log(res)
                wx.showToast({
                    title: '加载成功',
                    icon: "none"
                })
                this.setData({
                    address: res.result.address,
                    fullAddress: res.result.address
                })
            }

        })
    },

    changeadres(e) {
        console.log(e)
        const [provinceCode, cityCode, districtCode] = e.detail.code;
        const [provinceName, cityName, districtName] = e.detail.value;
        this.setData({
            provinceCode,
            cityCode,
            districtCode,
            provinceName,
            cityName,
            districtName

        })
    },
    async baocun() {
        console.log(this.data.name)
        let data = this.data
        const res = await instance.post("/userAddress/update", data);
        console.log(res);
        if (res.code == 200) {
            wx.navigateBack()
        }
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})